package com.dodo.blog.ui.component.repeater;

import com.dodo.blog.ui.component.Component;
import com.dodo.blog.ui.component.container.Td;
import com.dodo.blog.ui.component.container.Th;
import com.dodo.blog.ui.component.container.Tr;
import com.dodo.blog.ui.component.input.Anchor;
import com.dodo.blog.ui.util.FormatUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
public abstract class Table<T>
        extends Repeater<T>
{
    private static final long serialVersionUID = 1L;

    private Tr content;

    @SuppressWarnings( value = "unchecked" )
    public Table( String id, IDataSource<T> dataSource )
    {
        super( id, dataSource, Tag.TABLE );
    }

    protected void populateRows()
    {
        createTableHeaders( getTableHeaders() );

        List<T> rows = getRows();

        for ( T row : rows )
        {
            content = new Tr();
            super.add( content );

            populate( row );
        }
    }

    private void createTableHeaders( TableHeader[] tableHeaders )
    {
        Tr header = new Tr();
        add( header );

        for ( TableHeader tableHeader : tableHeaders )
        {
            Th col = new Th();
            header.add( col );

            if ( tableHeader.getName() != null )
            {
                Anchor a = new Anchor();
                a.setAttribute( ATTR_REPEATER_ID, getId() );
                a.addClassName( CLASS_REPEATER_LINK );
                a.setInnerHtml( tableHeader.getLabel() );

                OrderDirection orderDirection = OrderDirection.ASC;
                if ( tableHeader.getName().equals( getOrderBy() ) )
                {
                    col.addClassName( "sort" );
                    col.addClassName( getOrderDirection().name().toLowerCase() );
                    if ( getOrderDirection() == OrderDirection.ASC )
                    {
                        orderDirection = OrderDirection.DESC;
                    }
                }
                a.setHref( createUrl( getFirstRow(), getMaxRows(), tableHeader.getName(), orderDirection ) );

                col.add( a );
            }
            else
            {
                col.setInnerHtml( tableHeader.getLabel() );
            }
        }
    }

    public abstract TableHeader[] getTableHeaders();

    public void addColumn( Component component )
    {
        Td col = new Td();
        col.add( component );
        content.add( col );
    }

    public void addColumn( String text )
    {
        Td col = new Td();
        col.setInnerHtml( text );
        content.add( col );
    }

    public void addColumn( Boolean bool )
    {
        Td col = new Td();
        col.setInnerHtml( bool.toString() );
        content.add( col );
    }

    public void addColumn( Date date, SimpleDateFormat format )
    {
        Td col = new Td();
        col.setInnerHtml( FormatUtil.formatDate( date, format ) );
        content.add( col );
    }

    public void addColumn( Component... components )
    {
        Td col = new Td();
        for ( Component c : components )
        {
            col.add( c );
        }

        content.add( col );
    }

}